/*
 * @Description: mark
 * @Author: wangwangwang
 * @Date: 2020-07-23 15:01:32
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-02 17:47:26
 */

/**
 * @Description: 参数
 * @Param: 
 * @Author: wangwangwang
 */
export default {
  Constant: {
    hanldeMenu: [{
      name: '抄送',
      id: '1',
    }, {
      name: '退回',
      id: '2',
    }, {
      name: '交办',
      id: '3',
    }, {
      name: '协审',
      id: '4',
    }, {
      name: '加签',
      id: '5',
    }, {
      name: '减签',
      id: '6',
    }, {
      name: '导出数据',
      id: '7',
    }],
  }
}
/**
 * @Description: 格式化Tree数据
 * @Param: {source, id, parentId, children, firstLayer}格式化的数据；id子ID；parentId父ID；children要生成的子对象名；firstLayer第一层父ID
 * @Author: wangwangwang
 */
export const formatTreeData = (source, id, parentId, children, firstLayer) => {
  const cloneData = JSON.parse(JSON.stringify(source))
  if (cloneData.length) {
    return cloneData.filter(father => {
      const fathercopy = father
      const branchArr = cloneData.filter(child => father[id] === child[parentId])
      if (branchArr.length > 0) {
        fathercopy[children] = branchArr
      }
      return father[parentId] === firstLayer
    })
  }
  return []
}
export const formatOrganization = (data, id) => {
  console.log(data);
  const copyData = JSON.parse(JSON.stringify(data));
  const newArry = [];
  const findOrganization = (findData, findId) => {
    for (let i = 0; i < findData.length; i += 1) {
      if (findData[i].id === findId) {
        newArry.unshift(findData[i]);
        if (findData[i].parentId !== 'parent_id') {
          findOrganization(findData, findData[i].parentId)
        } else {
          return newArry
        }
      }
    }
  }
  if (copyData.length) {
    find()
    findOrganization(copyData, id)
  }
  return newArry
}

/**
 * @Description: 格式化table数据  数据存在没有id及pid关系时
 * @Param: {source, id, parentId, children, firstLayer}格式化的数据；id子ID；parentId父ID；children要生成的子对象名；firstLayer第一层父ID
 * @Author: wangwangwang
 */

export const transData = (a, idStr, pidStr, chindrenStr) => {
  const r = [];
  const hash = {};
  const id = idStr;
  const pid = pidStr;
  const children = chindrenStr;
  let i = 0;
  let j = 0;
  const len = a.length;
  for (; i < len; i += 1) {
    hash[a[i][id]] = a[i];
  }
  for (; j < len; j += 1) {
    const aVal = a[j];
    const hashVP = hash[aVal[pid]];
    if (hashVP) {
      !hashVP[children] && (hashVP[children] = []);
      hashVP[children].push(aVal);
    } else {
      r.push(aVal);
    }
  }
  return r;
}

/*去除完全重复的*/
export const arrRepeat = (obj) => {
  obj.forEach((item, index) => {
    let str = JSON.stringify(item)
    obj[index] = str
  })
  var newArr = [...new Set(obj)]
  newArr.forEach((item, index) => {
    let json = JSON.parse(item)
    newArr[index] = json
  })
  return newArr
}
/*根据str去重*/
export const unique = (arr, str) => {
  // 根据唯一标识orderId来对数组进行过滤
  const res = new Map(); //定义常量 res,值为一个Map对象实例 //返回arr数组过滤后的结果，结果为一个数组   过滤条件是，如果res中没有某个键，就设置这个键的值为1
  return arr.filter(arr => !res.has(arr[str]) && res.set(arr[str], 1));
}
/*设置获取流程节点ID*/
export const getNodeId = () => {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

export const _debounce = (fn, wait) => {
  const delay = wait || 200
  var timer
  return function () {
    const th = this
    const args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(function () {
      timer = null
      fn.apply(th, args)
    }, delay)
  }
}